import { Plugin } from 'prosemirror-state'

export const ShortcutCopyPlugin = (editor) => new Plugin({
  props: {
    handleKeyDown(view, event) {
      if (event.ctrlKey && event.key.toLowerCase() === 'a') {
        document.getSelection()?.selectAllChildren(view.dom)
        return true
      }
      if (event.ctrlKey && event.key.toLowerCase() === 'c') {
        const html = editor.getHTML()
        const blob = new Blob([html], { type: 'text/html' })
        navigator.clipboard.write([new ClipboardItem({ 'text/html': blob })])
        event.preventDefault()
        return true
      }
  return false
    }
  }
})
